package cn.cool.domain.activity.service.discount.impl;

import cn.cool.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.cool.domain.activity.service.discount.AbstractDiscountCalculateService;
import cn.cool.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("MJ") // 满减的策略
@Slf4j
public class MJCalculateService extends AbstractDiscountCalculateService {
    @Override
    protected BigDecimal doCalculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        // 获取计算公式
        String marketExpr = groupBuyDiscount.getMarketExpr();
        String[] split = marketExpr.split(Constants.SPLIT);
        BigDecimal x = new BigDecimal(split[0].trim());
        BigDecimal y = new BigDecimal(split[1].trim());

        // 计算是否满足满减的条件，即 {原价格 >= x}
        if (originalPrice.compareTo(x) < 0) {
            return originalPrice;
        }

        return originalPrice.subtract(y);
    }
}
